// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: donggu

#pragma once

#include <functional>
#include <vector>

#include <glog/logging.h>

#include "coin2/exchange/base/executor/actor.h"
#include "coin2/exchange/base/executor/topic_distributor.h"

namespace coin2::exchange::base::executor {

// schedule async actors
class AsyncExecutor {
 public:
  virtual ~AsyncExecutor() = default;

  virtual void Start() = 0;
  virtual void Stop() = 0;

  TopicRecordDistributor* distributor() { return &topic_distributor_; }
  size_t NumSystems() const { return subsystems_.size(); }
  void RegisterSubsystem(IAsyncActor* subsystem) { subsystems_.push_back(subsystem); }

  void StartAsync() {
    LOG(INFO) << "[AsyncExecutor] num of subsystems: " << subsystems_.size();
    for (auto& actor : subsystems_) {
      actor->StartAsync();
    }
  }
  void StopAsync() {
    LOG(INFO) << "[AsyncExecutor] num of subsystems: " << subsystems_.size();
    for (auto& actor : subsystems_) {
      actor->StopAsync();
    }
  }

 protected:
  TopicRecordDistributor topic_distributor_;
  std::vector<IAsyncActor*> subsystems_;
};

}  // namespace coin2::exchange::base::executor
